//
//  TipsViewController.m
//  PiPiPineapple
//
//  Created by 李晓东 on 17/11/15.
//  Copyright © 2017年 PeanutXu. All rights reserved.
//

#import "TipsViewController.h"
#import <Masonry.h>
@interface TipsViewController ()
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) NSString *tipsContent;
@end

@implementation TipsViewController

- (instancetype)initWithTipsContent:(NSString *)tipContent{
    if (self = [super init]) {
        _tipsContent = tipContent;
    }
    return self;
}

- (void)loadView{
    [super loadView];
    _textView = [[UITextView alloc]init];
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:_tipsContent];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, _tipsContent.length)];
    [self changeAttributeColor:@"篮球" color:[UIColor blueColor] attributeString:attributeStr];
    [self changeAttributeColor:@"红球" color:[UIColor redColor] attributeString:attributeStr];
    [self changeAttributeColor:@"红色球" color:[UIColor redColor] attributeString:attributeStr];
   
    
    [self.textView setAttributedText:attributeStr];
    [self.view addSubview:_textView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(self.view.mas_left);
        make.width.mas_equalTo(self.view.mas_width);
        make.height.mas_equalTo(self.view.mas_height);
    }];
}

- (void)changeAttributeColor:(NSString *)targetStr color:(UIColor *)color attributeString:(NSMutableAttributedString *)attributeStr{
    
    NSArray *listItems =  [_tipsContent componentsSeparatedByString:targetStr];
    for (NSString *item in [listItems subarrayWithRange:NSMakeRange(0, listItems.count - 1)]) {
        NSRange range = [_tipsContent rangeOfString:item];
        [attributeStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(range.location + range.length, targetStr.length)];
        [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(range.location + range.length, targetStr.length)];
        
    }
}

@end
